package com.woniuxy.commons.utils;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

/**
 * @Author weizihan
 * @Date 2020/7/17 2020/7/17
 * @Description 文件上传工具类
 * @Since version-1.0
 */
public class FilesUpload {

    //工具固定格式解析Excel
    public static Map<String,Object> analysisExcel(String url) throws IOException {
        //接受信息
        Map<String, Object> map = new LinkedHashMap();
        //标志符
        int i = 0;
        //创建工作铺
        XSSFWorkbook sheets = new XSSFWorkbook(new FileInputStream(url));
       // System.out.println(sheets);
        //读取Excel表
        XSSFSheet rows = sheets.getSheetAt(0);
       // System.out.println(rows);
        //获取总行数,从0开始
        int sum = rows.getLastRowNum();
       // System.out.println(sum);
        //循环获取每各数据
        if (sum != 0){
            for(int row = 0 ; row <= sum ; row ++){
                if (rows.getRow(row) != null){
                    int maxRol = rows.getRow(row).getLastCellNum();
                    System.out.println();
                    for (int rol = 0 ; rol < maxRol ; rol ++){
                        //map(key="表头+主键"，value="内容")
                        if (rows.getRow(row).getCell(rol) != null) {
                            map.put(rows.getRow(0).getCell(rol).toString() +","+ i, rows.getRow(row).getCell(rol));
                        }
                    }
                    i ++;
                }
            }
        }
        return  map;
    }


//    public static void main(String[] args) throws IOException {
//        System.out.println(analysisExcel("D://新建文件夹/e5ca80aa-b73b-4411-b0c3-d3dd1c96013f.xlsx"));;
//
//    }
}
